package com.bookstore.listener;

import com.bookstore.entity.User;
import com.bookstore.service.UserService;
import com.bookstore.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.Date;

@WebListener
public class MyAttributeListener implements HttpSessionAttributeListener {
    UserService userService = new UserServiceImpl();

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        if ("user".equals(event.getName())) {
            //监听用户登录成功
            //1.拿到本次传入的用户信息
            User user = (User) event.getValue();
            userService.updateLastLoginTime(user.getId().longValue(), new Date());
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {

    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {

    }
}
